package com.you.open.api.demo.controller;

import com.you.open.api.demo.entity.ResponseVo;
import com.you.open.api.demo.service.IOpenApiService;
import com.you.simple.demo.api.entity.UserInfo;
import com.yough.api.framework.bean.ApiDocumentBean;
import com.yough.api.framework.constant.DocumentConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author yough
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/1/13
 */
@Controller
public class OpenApiIndexController {

    @Autowired
    private IOpenApiService openApiService;

    @RequestMapping("/openApiIndex")
    public String openApiIndex(Model model) {
        ConcurrentHashMap<String, ApiDocumentBean> apiDocumentBeanMapVo = DocumentConstants.apiDocumentBeanMap;
        System.out.println(apiDocumentBeanMapVo);
        model.addAttribute("apiDocumentBeanMapVo", apiDocumentBeanMapVo);
        return "index";
    }

    @RequestMapping("/getApiDetailInfo")
    public String getApiDetailInfo(@RequestParam("apiId") String apiId, Model model) {
        System.out.println("------------" + apiId);
        ConcurrentHashMap<String, ApiDocumentBean> apiDocumentBeanMap = DocumentConstants.apiDocumentBeanMap;
        ApiDocumentBean apiDocumentBean = apiDocumentBeanMap.get(apiId);
        System.out.println(apiDocumentBean);
        model.addAttribute("apiDocumentBean", apiDocumentBean);
        model.addAttribute("apiId", apiId);
        return "apiDetailInfo";
    }


    @RequestMapping("/request")
    @ResponseBody
    public ResponseVo request(@RequestBody Map<String, Object> requestMap) {
        System.out.println("----------得到的 requestMap 是：---------");
        System.out.println(requestMap);
        ResponseVo responseVo = null;
        Object resultObject = null;
        try {
            responseVo = openApiService.apiService(requestMap);
            Thread.sleep(1000L);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return responseVo;
    }
}
